<?php
class PNFile extends PNObjectEx {
  function PNFile($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'votedatacenter_file';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';


    $this->_init($init, $where);
  }


    ///////////////////////// POST PROCESS ///////////////////////////
  function insertPostProcess(){
    Loader::loadClass('DataUtilEx', "modules/InnoAdvert/pnincludes");
    $id = DBUtil::getInsertID ($this->_objType, $this->_objField);
    if ($id){
      $object = array('id'=>$id);
      DBUtil::updateObject($object,$this->_objType);
      $this->uploadFiles($id);
    }
  }

  function updatePostProcess(){
    //set thumb to image status
    if ($_FILES['files'][size]!=0 && $_FILES['files'][error]==0){
            $form = FormUtil::getPassedValue ('form', false );
            $id = $this->_objData['id'];

            //delete file
            foreach($form['file_delete'] as $delete_id){
              DBUtil::deleteObjectByID('votedatacenter_file',$delete_id);
            }

            if ($id){
              //delete path in table
              $objects = array('id','iconpath');
              $objectsName = DBUtil::selectObject( 'votedatacenter_file', "WHERE ctg_id = '$id'",
                                     $objects, null, null, true );
              $path = $objectsName['iconpath'];

             unlink($path);
            }
             //delete file from update


             $this->uploadFiles($id);
   }
  }



  function deletePreProcess(){
    $id = $this->_objData['id'];
    
    if ($id){                         
      //delete path in table
      $objects = array('id','iconpath');
      $objectsName = DBUtil::selectObject( 'votedatacenter_file', "WHERE ctg_id = '$id'",
                             $objects, null, null, true );
      $path = $objectsName['iconpath'];
      
      unlink($path);
     
      //delete files
      DBUtil::deleteWhere('votedatacenter_file',"WHERE ctg_id = '$id'");
    }
  }


  function uploadFiles($id){
    if ($id && $_FILES['files']){
      $icon = $_FILES['files'];
      $rootpath = "pnTemp/votedatacenter_upload/csv";
        if ($_FILES['files']){
            $count = DBUtil::selectObjectCount ('votedatacenter_file', "WHERE ctg_id = '$id' AND ctg_iconpath = '$rootpath'");
            $iconname = $icon["name"];
            $tmp_name = $icon["tmp_name"];

////////////////////////// get the extension of the file in a lower case format/////////////////////////////////////
            $iconname = stripslashes($iconname);
            $i = strrpos($iconname,".");
            if (!$i) { return ""; }
            $l = strlen($iconname) - $i;
            $ext = substr($iconname,$i+1,$l);
            $extension = strtolower($ext);

////////////////////////////generate new file&path//////////////////////////////////////////////////////////////////
            $rootname       = $next_id.time().".".$extension;
            $iconname_temp  = "images_".$id.$rootname;
            $path           = $rootpath.$iconname_temp;


            copy($tmp_name,$path);           
            $objects = array(
                'id' => $id,
                'iconpath'=> $path);

          DBUtil::updateObject($objects,'votedatacenter_file'); 
        }
      
    }
  }

}